﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
public partial class ShowLeaguerInfo : System.Web.UI.Page
{
    Operation operation = new Operation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"].ToString();
            try
            {
            DataSet ds = operation.SelectPublicInfoById(Convert.ToInt32(id), true);
            
                DataSet ds2 = operation.SelectUserInfo(ds.Tables[0].Rows[0][8].ToString());
                lbPrjMan.Text = ds.Tables[0].Rows[0][4].ToString();
                lbPrjName.Text = ds.Tables[0].Rows[0][2].ToString();
                lbWayReq.Text = ds.Tables[0].Rows[0][3].ToString();
                lbTel.Text = ds.Tables[0].Rows[0][5].ToString();
                lbComName0.Text = ds.Tables[0].Rows[0][10].ToString();
                lbDept.Text = ds.Tables[0].Rows[0][11].ToString();
                lbRanks.Text = ds.Tables[0].Rows[0][12].ToString();
                lbAddress0.Text = ds.Tables[0].Rows[0][13].ToString();
                lbPostCode.Text = ds.Tables[0].Rows[0][14].ToString();
                lbPhone.Text = ds.Tables[0].Rows[0][15].ToString();
                lbFx.Text = ds.Tables[0].Rows[0][16].ToString();
                lbEmail.Text = ds.Tables[0].Rows[0][17].ToString();
                lbProblem.Text = ds.Tables[0].Rows[0][18].ToString();
                lbComInfo.Text = ds.Tables[0].Rows[0][19].ToString();
                lbNewPrd.Text = ds.Tables[0].Rows[0][20].ToString();
                lbWill.Text = ds.Tables[0].Rows[0][21].ToString();

                if (Session["userid"] != null)
                {
                    DataSet ds3 = operation.SelectUserInfo(Session["userid"].ToString());
                    txtMsgMail.Text = ds3.Tables[0].Rows[0][4].ToString();
                    txtMsgName.Text = ds3.Tables[0].Rows[0][1].ToString();
                    txtMsgTel.Text = ds3.Tables[0].Rows[0][5].ToString();
                }
            }
            catch (Exception exp) { }
           /* txtType.Text = ds.Tables[0].Rows[0][1].ToString();
            txtTitle.Text = ds.Tables[0].Rows[0][2].ToString();
            txtInfo.Text = ds.Tables[0].Rows[0][3].ToString();
            txtLinkMan.Text = ds.Tables[0].Rows[0][4].ToString();
            txtTel.Text = ds.Tables[0].Rows[0][5].ToString();
            date.Text = ds.Tables[0].Rows[0][7].ToString();
            txtMsgTitle.Text = "我对贵公司的" + txtTitle.Text + "感兴趣";
            if (ds.Tables[0].Rows[0][9].ToString().CompareTo("0") != 0)
            {
                txtType.Text = txtType.Text + "--" +
                    operation.SelectCategoryByID(ds.Tables[0].Rows[0][9].ToString()).Tables[0].Rows[0][1].ToString();
            }*/
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //发送留言邮件和在用户留言表插入留言
        try
        {
            string id = Request.QueryString["id"].ToString();
            operation.InsertNote(txtMsgTitle.Text.Trim().Length > 24 ? txtMsgTitle.Text.Substring(0, 24) : txtMsgTitle.Text.Trim(), txtMsgInfo.Text.Trim(), txtMsgName.Text.Trim(), txtMsgTel.Text.Trim(), txtMsgMail.Text.Trim(), id);
            DataSet ds = operation.SelectPublicInfoById(Convert.ToInt32(id), true);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ds = operation.SelectUserInfo(ds.Tables[0].Rows[0][8].ToString());
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string mail = ds.Tables[0].Rows[0][4].ToString();
                    string msg = "留言标题：" + txtMsgTitle.Text + "\n" +
                    "详细内容：" +
                    txtMsgInfo.Text + "\n" + "留言人姓名：" +
                   txtMsgName.Text + "\n" + "留言人电话：" +
                    txtMsgTel.Text + "\n" + "留言人邮箱：" + txtMsgMail.Text;
                    sendMail(mail, "你好，以下是科研成果超市客户给你的留言\n\n" + msg);
                }
            }
        }
        catch (Exception ex)
        {
            WebMessageBox.Show("发送留言失败，请重试！");
            return;
        }
        WebMessageBox.Show("留言成功！", "Default.aspx");

    }
    public void sendMail(string toEmailstring, string body)
    {
        string FROM = "dmdm2802@gmail.com";
        string SUBJECT = "留言信息";
        string BODY = body;
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(FROM, "科研成果超市");
        msg.To.Add(toEmailstring);
        msg.Subject = SUBJECT;
        msg.Body = BODY;
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.IsBodyHtml = false;
        msg.ReplyTo = new MailAddress(FROM);
        //smtp.126.com不可用，所有使用smtp.gmail.com
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new NetworkCredential("dmdm2802", "<pwd>2010");
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(msg);
        }
        catch (Exception e) { }
    }
}
